﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;

namespace Video
{
    /// <summary>
    ///     提供了基础的视频协议解析器
    /// </summary>
    class VideoProtocolParser : IParser
    {
        #region IParser 成员

        public bool Accpet(Dictionary<string, byte[]> pck)
        {
            return PackageHelper.GetProtocol(pck) == Hi_Baidu.Packet.Protocol.NetworkProtocol.VIDEO;
        }

        public int GetLength(byte[] pckBody)
        {
            return pckBody.Length;
        }

        public bool NeedAck(Dictionary<string, byte[]> pck)
        {
            bool Result = Encoding.Default.GetString(pck["ACK"]) == "0" ? false : true;
            return Result;
        }

        public InPackage ParserInPackage(Dictionary<string, byte[]> PckContent)
        {
            BaseInPackage BIP = new BaseInPackage();
            //ACK Property
            if (Encoding.Default.GetString(PckContent["ACK"]) == "0")
            {
                BIP.IS_ACK = false;
            }
            else
            {
                BIP.IS_ACK = true;
            }
            //Encrypt Property
            if (Encoding.Default.GetString(PckContent["ENCRYPT"]) == "0")
            {
                BIP.IS_ENCRYPT = false;
            }
            else
            {
                BIP.IS_ENCRYPT = true;
            }

            BIP.Protocol = PackageHelper.GetProtocol(PckContent);
            BIP.SendTime = DateTime.Parse(Encoding.Default.GetString(PckContent["SENDTIME"]));
            BIP.Header = Encoding.Default.GetString(PckContent["TYPE"]);
            BIP.Body = PckContent;
            BIP.Command = PackageHelper.GetNetworkCommand(PckContent);
            return BIP;
        }

        #endregion
    }
}
